package com.pig4cloud.pig.mt.manage;

import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.mt.form.WxUserBatchTaggingForm;
import com.pig4cloud.pig.mt.form.WxUserTagForm;
import com.pig4cloud.pig.mt.service.WxUserTagsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.tag.WxUserTag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/manage/wxUserTags")
@Api(value = "/manage/wxUserTags", tags = "公众号用户标签管理")
public class WxUserTagsManageController {
    @Autowired
    private WxUserTagsService wxUserTagsService;
    @Autowired
    private WxMpService wxMpService;

    /**
     * 查询用户标签
     */
    @ApiOperation(value = "查询用户标签", notes = "查询用户标签")
    @GetMapping("/list")
	@PreAuthorize("@pms.hasPermission('mt_wxuser_get')" )
    public R list(@CookieValue String appid) throws WxErrorException {
        wxMpService.switchoverTo(appid);
        List<WxUserTag> wxUserTags = wxUserTagsService.getWxTags();
        return R.ok(wxUserTags);
    }

    /**
     * 修改或新增标签
     */
    @ApiOperation(value = "修改或新增标签", notes = "修改或新增标签")
	@SysLog("修改或新增标签")
    @PostMapping("/save")
	@PreAuthorize("@pms.hasPermission('mt_wxuser_add')" )
    public R save(@CookieValue String appid, @RequestBody WxUserTagForm form) throws WxErrorException {
        wxMpService.switchoverTo(appid);
        Long tagid = form.getId();
        if (tagid == null || tagid <= 0) {
            wxUserTagsService.creatTag(form.getName());
        } else {
            wxUserTagsService.updateTag(tagid, form.getName());
        }
        return R.ok();
    }

    /**
     * 删除标签
     */
	@ApiOperation(value = "删除标签", notes = "删除标签")
	@SysLog("删除标签")
    @DeleteMapping("/delete/{tagid}")
	@PreAuthorize("@pms.hasPermission('mt_wxuser_del')" )
    public R delete(@CookieValue String appid, @PathVariable("tagid") Long tagid) throws WxErrorException {
        if (tagid == null || tagid <= 0) {
            return R.failed("标签ID不得为空");
        }
        wxMpService.switchoverTo(appid);
        wxUserTagsService.deleteTag(tagid);
        return R.ok();
    }

    /**
     * 批量给用户打标签
     */
    @ApiOperation(value = "批量给用户打标签", notes = "批量给用户打标签")
	@SysLog("批量给用户打标签")
    @PostMapping("/batchTagging")
	@PreAuthorize("@pms.hasPermission('mt_wxuser_edit')" )
    public R batchTagging(@CookieValue String appid, @RequestBody WxUserBatchTaggingForm form) throws WxErrorException {
        wxMpService.switchoverTo(appid);
        wxUserTagsService.batchTagging(form.getTagid(), form.getOpenidList());
        return R.ok();
    }

    /**
     * 批量移除用户标签
     */
    @ApiOperation(value = "批量移除用户标签", notes = "批量移除用户标签")
    @SysLog("批量移除用户标签")
    @PostMapping("/batchUnTagging")
	@PreAuthorize("@pms.hasPermission('mt_wxuser_edit')" )
    public R batchUnTagging(@CookieValue String appid, @RequestBody WxUserBatchTaggingForm form) throws WxErrorException {
        wxMpService.switchoverTo(appid);
        wxUserTagsService.batchUnTagging(form.getTagid(), form.getOpenidList());
        return R.ok();
    }
}
